package com.abe.libquick.base.mvp;

import com.abe.libcore.utils.CommonUtils;

import org.jetbrains.annotations.NotNull;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BaseModel {
    public static class SimpleCallback<T> implements Callback<T> {
        private ResultListener<T> listener;

        public SimpleCallback(ResultListener<T> listener) {
            this.listener = listener;
        }

        @Override
        public void onResponse(@NotNull Call<T> call, @NotNull Response<T> response) {
            if (listener != null) {
                listener.onResult(response.body());
                listener.onFinish();
            }
        }

        @Override
        public void onFailure(@NotNull Call<T> call, @NotNull Throwable t) {
            if (listener != null) {
                listener.onFail(t.getMessage());
                listener.onFinish();
            }
        }
    }

    public static class SimStartCallback<T> implements Callback<T> {
        private SimResultListener<T> listener;

        public SimStartCallback(SimResultListener<T> listener) {
            this.listener = listener;
            CommonUtils.INSTANCE.log("SimStartCallback-start" + System.currentTimeMillis());
            if (this.listener != null) {
                this.listener.onStart();
            }
        }

        @Override
        public void onResponse(@NotNull Call<T> call, @NotNull Response<T> response) {
            if (listener != null) {
                listener.onResult(response.body());
                listener.onFinish();
            }
        }

        @Override
        public void onFailure(@NotNull Call<T> call, @NotNull Throwable t) {
            if (listener != null) {
                listener.onFail(t.getMessage());
                listener.onFinish();
            }
        }
    }
}